﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers
{
    public class Order_EvaController : Controller
    {
        //
        // GET: /Order_Eva/

        ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取系统的增删查改的权限
        /// </summary>
        /// <returns></returns>
        public ActionResult getPower()
        {
            try
            {
                string[] PowerS = null;
                //string RetPower = "";
                Models.ControlPower RetPower = new Models.ControlPower();
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
                if (user != null)
                {
                    PowerS = user.RolePower.Split(',');//权限码
                    //拼接增删查改的权限
                    foreach (string pow in PowerS)
                    {
                        if (pow == "009008003")
                        {
                            RetPower.Dell = 1;
                        }
                    }
                }
                else
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                return Json(RetPower);
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取当前操作员拥有商户的权限
        /// </summary>
        /// <returns></returns>
        public ActionResult getMer(JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.Order_Mer_T> page = new Page<Model.Order_Mer_T>();
                List<Model.Order_Mer_T> list = new List<Model.Order_Mer_T>();//定义商品分类设置
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = service.GetMer(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            Model.Order_Mer_T m = new Model.Order_Mer_T();
                            m.MerID = item["MerID"].ToString();
                            m.MerName = item["MerName"].ToString();
                            m.MerAdr = item["MerAdr"].ToString();
                            m.MerCall = item["MerCall"].ToString();
                            m.MerTxt = item["MerTxt"].ToString();
                            list.Add(m);
                        }
                    }
                    jqgridparam.records = list.Count;//查询的数据总数
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = page.FindListPage(list, ref jqgridparam)//返回当前页的数据
                    };
                    return Json(data);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取所有订单的评价信息
        /// </summary>
        /// <param name="MerID"></param>
        /// <returns></returns>
        public List<Model.View_Order_Eva> GetAllEva(string MerID)
        {
            List<Model.View_Order_Eva> list = new List<Model.View_Order_Eva>();
            string result = service.GetOrder_Eva(1,5000,MerID, "", 1);
            JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
            string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
            if (bs == "1")//返回结果正常
            {
                IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                if (jieguo.Count > 0)
                {
                    foreach (JToken item in jieguo)
                    {
                        // List<Model.Sys_Component_T> list_com = new List<Model.Sys_Component_T>();
                        List<Model.View_Order_Eva> list_Dishes = new List<Model.View_Order_Eva>();
                        Model.View_Order_Eva m = new Model.View_Order_Eva();
                        m.FormID = item["FormID"].ToString();
                        m.EvaID = int.Parse(item["EvaID"].ToString());
                        m.MerName = item["MerName"].ToString();
                        m.DishesID = item["DishesID"].ToString();
                        m.MerID = item["MerID"].ToString();
                        m.AccID = int.Parse(item["AccID"].ToString());
                        m.EvaTxt = item["EvaTxt"].ToString();
                        m.EvaTime = item["EvaTime"].ToString();
                        m.EvaStars = int.Parse(item["EvaStars"].ToString());
                        m.MerTxt = item["MerTxt"].ToString();
                        m.BZ = item["BZ"].ToString();
                        m.DishesNmae = item["DishesNmae"].ToString();
                        m.AccName = item["AccName"].ToString();
                        list.Add(m);
                    }
                }
                return list;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获取商户的订单评价信息
        /// </summary>
        /// <param name="MerID">商户ID</param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult getgrid(string MerID, JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.View_Order_Eva> page = new Page<Model.View_Order_Eva>();//分页类定义
                List<Model.View_Order_Eva> list = new List<Model.View_Order_Eva>();
                //list = GetAllEva(MerID);
                string result = service.GetOrder_Eva(jqgridparam.page,jqgridparam.rows, MerID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        list = ParseClass<Model.View_Order_Eva>.JsonList(jieguo);//解析返回的结果
                        foreach (Model.View_Order_Eva item in list)
                        {
                            item.EvaTime = DateTimeHelper.ToDate2(item.EvaTime);
                            if (item.EvaTime.Length == 14)
                            {
                                item.EvaTime = DateTimeHelper.ToDate2(item.EvaTime);
                            }
                        }
                    }
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = "0",
                        rows = list
                    };
                    return Json(data);
                }
                //if (list.Count > 0)
                //{
                //    jqgridparam.records = list.Count;//查询的数据总数
                //    var data = new
                //    {
                //        total = list.Count.ToString(),
                //        rows = page.FindListPage(list, ref jqgridparam)
                //    };
                //    return Json(data);
                //}
                //else
                //{
                //    var data = new
                //    {
                //        total = "0",
                //        rows = list
                //    };
                //    return Json(data);
                //}
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 增、删、改商品评价信息
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateOrder_Eva(int Tag, Model.Order_Eva_T Order_Eva)
        {
            try
            {
                string result = "";
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string str1 = Order_Eva.EvaTime.Replace("/", "");
                string str2 = str1.Replace(":", "");
                string str3 = str2.Replace(" ", "");
                Order_Eva.EvaTime = str3;
                string json = Order_Eva.ToJson();
                result = service.SetOrder_Eva(user.OperID,json , Tag, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Content("操作异常：" + ex + "请联系管理员解决");
            }
        }

        #region 旧查询评价

        /// <summary>
        /// 查询评价
        /// </summary>
        /// <param name="MerID">商户ID</param>
        /// <param name="Dishes">商品名称</param>
        /// <param name="FormID">订单号</param>
        /// <param name="Star">星级</param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        //public ActionResult searchEva(string MerID, string Dishes, string FormID, int Star, JqGridParam jqgridparam)
        //{
        //    try
        //    {
        //        Page<Model.View_Order_Eva> page = new Page<Model.View_Order_Eva>();
        //        List <Model.View_Order_Eva> ls = GetAllEva(MerID);
        //        if (Dishes != "")
        //        {
        //           ls= ls.Where(i => i.DishesNmae.IndexOf(Dishes) != -1).ToList();
        //        }
        //        if (FormID != "")
        //        {
        //           ls= ls.Where(i => i.FormID == FormID).ToList();
        //        }
        //        if (Star == -8989)
        //        {
        //            ls = ls.ToList();
        //        }
        //        if (Star != 0 && Star != -8989)
        //        { ls = ls.Where(i => i.EvaStars == Star).ToList(); }
        //        jqgridparam.records = ls.Count;//查询的数据总数
        //        var data = new
        //        {
        //            total = ls.Count,
        //            rows = page.FindListPage(ls, ref jqgridparam)//返回当前页的数据 
        //        };
        //        return Json(data);
        //    }
        //    catch (Exception) { return null; }
        //}
        #endregion

        #region 查询评价

        /// <summary>
        /// 查询评价
        /// </summary>
        /// <param name="MerID">商户ID</param>
        /// <param name="Dishes">商品名称</param>
        /// <param name="FormID">订单号</param>
        /// <param name="Star">星级</param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult searchEva(string MerID, string Dishes, string FormID, string Star, JqGridParam jqgridparam)
        {
            try
            {
                List<Model.View_Order_Eva> list = new List<Model.View_Order_Eva>();
                string result = service.GetOrder_EvaByPara(jqgridparam.page, jqgridparam.rows, FormID, Dishes, Star, MerID, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        list = ParseClass<Model.View_Order_Eva>.JsonList(jieguo);//解析返回的结果
                        foreach (Model.View_Order_Eva item in list)
                        {
                            item.EvaTime = DateTimeHelper.ToDate2(item.EvaTime);
                            if (item.EvaTime.Length == 14)
                            {
                                item.EvaTime = DateTimeHelper.ToDate2(item.EvaTime);
                            }
                        }
                    }
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = "0",
                        rows = list
                    };
                    return Json(data);
                }
            }
            catch (Exception) { return null; }
        }
        #endregion

    }
}
